home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.5 / contextlib.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-05-11  |  3.8 KB  |  173 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Utilities for with-statement contexts.  See PEP 343.'''
  5. import sys
  6. __all__ = [
  7.     'contextmanager',
  8.     'nested',
  9.     'closing']
  10.  
  11. class GeneratorContextManager(object):
  12.     '''Helper for @contextmanager decorator.'''
  13.     
  14.     def __init__(self, gen):
  15.         self.gen = gen
  16.  
  17.     
  18.     def __enter__(self):
  19.         
  20.         try:
  21.             return self.gen.next()
  22.         except StopIteration:
  23.             raise RuntimeError("generator didn't yield")
  24.  
  25.  
  26.     
  27.     def __exit__(self, type, value, traceback):
  28.         if type is None:
  29.             
  30.             try:
  31.                 self.gen.next()
  32.             except StopIteration:
  33.                 return None
  34.  
  35.             raise RuntimeError("generator didn't stop")
  36.         else:
  37.             
  38.             try:
  39.                 self.gen.throw(type, value, traceback)
  40.                 raise RuntimeError("generator didn't stop after throw()")
  41.             except StopIteration:
  42.                 exc = None
  43.                 return exc is not value
  44.             except:
  45.                 if sys.exc_info()[1] is not value:
  46.                     raise 
  47.                 
  48.  
  49.  
  50.  
  51.  
  52. def contextmanager(func):
  53.     '''@contextmanager decorator.
  54.  
  55.     Typical usage:
  56.  
  57.         @contextmanager
  58.         def some_generator(<arguments>):
  59.             <setup>
  60.             try:
  61.                 yield <value>
  62.             finally:
  63.                 <cleanup>
  64.  
  65.     This makes this:
  66.  
  67.         with some_generator(<arguments>) as <variable>:
  68.             <body>
  69.  
  70.     equivalent to this:
  71.  
  72.         <setup>
  73.         try:
  74.             <variable> = <value>
  75.             <body>
  76.         finally:
  77.             <cleanup>
  78.  
  79.     '''
  80.     
  81.     def helper(*args, **kwds):
  82.         return GeneratorContextManager(func(*args, **kwds))
  83.  
  84.     
  85.     try:
  86.         helper.__name__ = func.__name__
  87.         helper.__doc__ = func.__doc__
  88.         helper.__dict__ = func.__dict__
  89.     except:
  90.         (None,)
  91.  
  92.     return helper
  93.  
  94.  
  95. def nested(*managers):
  96.     '''Support multiple context managers in a single with-statement.
  97.  
  98.     Code like this:
  99.  
  100.         with nested(A, B, C) as (X, Y, Z):
  101.             <body>
  102.  
  103.     is equivalent to this:
  104.  
  105.         with A as X:
  106.             with B as Y:
  107.                 with C as Z:
  108.                     <body>
  109.  
  110.     '''
  111.     exits = []
  112.     vars = []
  113.     exc = (None, None, None)
  114.     
  115.     try:
  116.         for mgr in managers:
  117.             exit = mgr.__exit__
  118.             enter = mgr.__enter__
  119.             vars.append(enter())
  120.             exits.append(exit)
  121.         
  122.         yield vars
  123.     except:
  124.         exc = sys.exc_info()
  125.     finally:
  126.         while exits:
  127.             exit = exits.pop()
  128.             
  129.             try:
  130.                 if exit(*exc):
  131.                     exc = (None, None, None)
  132.             continue
  133.             exc = sys.exc_info()
  134.             continue
  135.  
  136.         if exc != (None, None, None):
  137.             raise exc[0], exc[1], exc[2]
  138.         
  139.  
  140.  
  141. nested = contextmanager(nested)
  142.  
  143. class closing(object):
  144.     '''Context to automatically close something at the end of a block.
  145.  
  146.     Code like this:
  147.  
  148.         with closing(<module>.open(<arguments>)) as f:
  149.             <block>
  150.  
  151.     is equivalent to this:
  152.  
  153.         f = <module>.open(<arguments>)
  154.         try:
  155.             <block>
  156.         finally:
  157.             f.close()
  158.  
  159.     '''
  160.     
  161.     def __init__(self, thing):
  162.         self.thing = thing
  163.  
  164.     
  165.     def __enter__(self):
  166.         return self.thing
  167.  
  168.     
  169.     def __exit__(self, *exc_info):
  170.         self.thing.close()
  171.  
  172.  
  173.